﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Web.admin.web
{
    public partial class WebLog : System.Web.UI.Page
    {
        static int startIndex = 1;
        static int endIndex;
        static int PageSize;
        static int PageCount;
        static int TotalCount;
        BLL.WebLog log = new BLL.WebLog();
        DbCommon.DbHelper dbHelper = new DbCommon.DbHelper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                RpLog_PageBind();
            }
        }
        /// <summary>
        /// rpLog分页数据绑定
        /// </summary>
        protected void RpLog_PageBind() {
            if (!string.IsNullOrEmpty(DbCommon.Utils.GetCookie("pageSize")))
            {
                PageSize = int.Parse(DbCommon.Utils.GetCookie("pageSize"));
            }
            else {
                PageSize = 8;
                DbCommon.Utils.writeToCookie("pageSize", PageSize.ToString());
            }
            TotalCount = dbHelper.GetCount("WebLog");
            PageCount = (TotalCount + PageSize - 1) / PageSize;
            if (!string.IsNullOrEmpty(Request.QueryString["start"]))
                startIndex = int.Parse(Request.QueryString["start"]);
            else
                startIndex = 1;
            endIndex = (startIndex + PageSize - 1) >= TotalCount ? TotalCount : (startIndex + PageSize - 1);
            RpLog.DataSource = dbHelper.GetPageTable("WebLog", startIndex, endIndex, "WbAddTime desc", "");
            RpLog.DataBind();
            tb_size.Text = PageSize.ToString();
            lb_pageNum.InnerHtml = editHtmlStr();
        }
        /// <summary>
        /// 编写html语句
        /// </summary>
        /// <returns></returns>
        protected string editHtmlStr() {
            StringBuilder stb = new StringBuilder();
            if (startIndex == 1)
            {
                stb.Append("<a class=\"a_btn_CCl a_btn_NCl\" href=\"#\">上一页</a>");
            }
            else
            {
                stb.Append("<a class=\"a_btn_CCl\" href=\"WebLog.aspx?start=" + (startIndex - PageSize) + "\">上一页</a>");
            }
            if (PageCount <= 10)
            {
                for (int j = 1; j <= PageCount; j++)
                {
                    if ((startIndex + PageSize - 1) / PageSize == j)
                    {
                        if (j == 1)
                        {
                            stb.Append("<a class=\"a_num_CCl a_num_NCl\" href=\"WebLog.aspx?start=1\">" + j + "</a>");
                        }
                        else
                            stb.Append("<a class=\"a_num_CCl a_num_NCl\" href=\"WebLog.aspx?start=" + ((j - 1) * PageSize + 1) + "\">" + j + "</a>");
                    }
                    else
                    {
                        if (j == 1)
                        {
                            stb.Append("<a class=\"a_num_CCl\" href=\"WebLog.aspx?start=1\">" + j + "</a>");
                        }
                        else
                            stb.Append("<a class=\"a_num_CCl\" href=\"WebLog.aspx?start=" + ((j - 1) * PageSize + 1) + "\">" + j + "</a>");
                    }
                }
            }
            else
            {
                for (int j = 1; j <= 10; j++)
                {
                    if (startIndex / PageSize + 1 == j)
                    {
                        if (j == 1)
                        {
                            stb.Append("<a class=\"a_num_CCl a_num_NCl\" href=\"WebLog.aspx?start=1\">" + j + "</a>");
                        }
                        else
                            stb.Append("<a class=\"a_num_CCl a_num_NCl\" href=\"WebLog.aspx?start=" + ((j - 1) * PageSize + 1) + "\">" + j + "</a>");
                    }
                    else
                    {
                        if (j == 1)
                        {
                            stb.Append("<a class=\"a_num_CCl\" href=\"WebLog.aspx?start=1\">" + j + "</a>");
                        }
                        else
                            stb.Append("<a class=\"a_num_CCl\" href=\"WebLog.aspx?start=" + ((j - 1) * PageSize + 1) + "\">" + j + "</a>");
                    }
                }
                stb.Append("<a class=\"a_num_CCl a_num_NCl\" href=\"#\">...</a>");

            }
            if ((startIndex + PageSize - 1) >= TotalCount)
            {
                stb.Append("<a class=\"a_btn_CCl a_btn_NCl\" href=\"#\">下一页</a>");
            }
            else
                stb.Append("<a class=\"a_btn_CCl\" href=\"WebLog.aspx?start=" + (startIndex + PageSize) + "\">下一页</a>");
            return stb.ToString();
        }
        /// <summary>
        /// tb_size文本值改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void tb_size_Click(object sender ,EventArgs e) {
            int _pageSize;
            if (int.TryParse(tb_size.Text, out _pageSize))
            {}
            else
                _pageSize = 8;
            DbCommon.Utils.writeToCookie("pageSize", _pageSize.ToString());
            RpLog_PageBind();
        }
        /// <summary>
        /// 批量删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int j = 0;
            for (int i = 0; i < RpLog.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)RpLog.Items[i].FindControl("lb_id")).Text);
                CheckBox cb = (CheckBox)RpLog.Items[i].FindControl("CbChoose");
                if (cb.Checked)
                {
                    if (log.delSingle(id)> 0)
                        j++;
                }
            }
            Response.Write("<script>alert('删除成功，共删除" + j + "条数据');window.location.href='WebLog.aspx'; </script>");
            return;
        }
    }
}